package com.ashin.digest;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

import org.junit.Test;

/**
 * md5 sha1等消息摘要，用于检验消息的完整性<br>
 * 
 * 消息摘要有两个基本属性：
 * <p>
 * 1. 两个不同的报文难以生成相同的摘要
 * <p>
 * 2. 难以对指定的摘要生成一个报文，而可以由该报文反推算出该指定的摘要
 * 
 * @author Ashin
 *
 */
public class Main {

    private String digestText = "hello md5 sha1 ";

    @Test
    public void encoderByMd5() throws NoSuchAlgorithmException, UnsupportedEncodingException {
        // 确定计算方法
        MessageDigest md5 = MessageDigest.getInstance("MD5");

        byte[] digest = md5.digest(digestText.getBytes("UTF-8"));
        System.out.println(Base64.getEncoder().encodeToString(digest));
    }
    
    @Test
    public void encoderBySha1() throws NoSuchAlgorithmException, UnsupportedEncodingException {
        MessageDigest sha1 = MessageDigest.getInstance("SHA-1");

        byte[] digest = sha1.digest(digestText.getBytes("UTF-8"));
        System.out.println(Base64.getEncoder().encodeToString(digest));
    }

}
